package com.yc.demo.d0316;

import com.yc.demo.util.DBHelper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class BooksDao {

    public List<Map<String,Object>> list = new LinkedList<>();

//    public List<Map<String,Object>> queryBook (){
//        String sql = "select * from books";
//        return  DBHelper.selectListMap(sql);
//    }
    //组合条件查询
    public List<Map<String,Object>> queryBook (String name,String author,int begin,int end){
        String sql = "select * from books where 1=1 ";
        List<Object> params = new ArrayList<>();
        if (name!=null &&name.trim().isEmpty()==false){
            sql += " and bookname like concat('%',?,'%') ";
            params.add(name);
        }
        if (author!=null &&author.trim().isEmpty()==false){
            sql += " and bookauthor like concat('%',?,'%') ";
            params.add(author);
        }
        params.add(begin);
        params.add(end);
        return  DBHelper.selectListMap(sql,params.toArray());
    }

    public Map<String,Object> getBook(String id){
        String sql = "select * from books where id = ?";
        return  DBHelper.selectOneMap(sql,id);
    }

    public void save(String id,String name,String press,String date,String author,String count){
        String sql ="update books set bookname = ?,bookpress = ?,pressdate = ?,bookauthor = ?,bookcount =? where id = ?";
        DBHelper.update(sql,name,press, date,author,count,id);
    }

}
